package com.zyl.file.result;

import lombok.Data;

import java.io.Serializable;

/**
 * @author zyl
 * @Desc
 * @date 2023/7/20 10:03
 */
@Data
public class Result<T> implements Serializable {

    private Integer code;
    private String msg;
    private T data;

    /**
     * 成功-无data
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        return result;
    }

    /***/
    public static <T> Result<T> success(T value) {
        Result<T> result = new Result<>();
        result.setData(value);
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setCode(ResultCode.SUCCESS.getCode());
        return result;
    }

    /***/
    public static <T> Result<T> success(T value, Integer code, String msg) {
        Result<T> result = new Result<>();
        result.setData(value);
        result.setMsg(msg);
        result.setCode(code);
        return result;
    }


    /***/
    public static <T> Result<T> error(T value, String msg, Integer code) {
        Result<T> result = new Result<>();
        result.setData(value);
        result.setMsg(msg);
        result.setCode(code);
        return result;
    }


    /***/
    public static <T> Result<T> error(ResultCode resultCode) {
        Result<T> result = new Result<>();
        result.setData(null);
        result.setMsg(resultCode.getMsg());
        result.setCode(resultCode.getCode());
        return result;
    }

    /***/
    public static <T> Result<T> error(String message, ResultCode resultCode) {
        Result<T> result = new Result<>();
        result.setData(null);
        if (null == message || 0 == message.length()) {
            result.setMsg(resultCode.getMsg());
        } else {
            result.setMsg(message);
        }
        result.setCode(resultCode.getCode());
        return result;
    }

    /**
     * 创建自定义的回传信息
     *
     * @param code 回执代号
     * @param msg  回执信息
     * @param <T>  数据类型
     * @return 实例
     */
    public static <T> Result<T> error(String msg, Integer code) {
        Result<T> result = new Result<>();
        result.setData(null);
        result.setMsg(msg);
        result.setCode(code);
        return result;
    }

    /***/
    public boolean isSucc() {
        return ResultCode.SUCCESS.getCode().equals(this.code);
    }

}
